//
//  NSDictionary+Category.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/11.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "NSDictionary+Category.h"


static NSString *const serverPublicKey = @"frapkkxookfjisksiakd26oo";

@implementation NSDictionary (Category)

- (NSDictionary *)encrypt {
    NSMutableDictionary *fullKeys = [[NSMutableDictionary alloc] init];
    NSString *signKey;
    if (self.count == 0) { // 只传token 没有其他参数的情况
        signKey = [NSString stringWithFormat:@"&key=%@",serverPublicKey];
    }else {
        signKey = [NSString stringWithFormat:@"%@&key=%@",[self serializeKeys],serverPublicKey];
    }
    signKey = [[signKey MD5Encode] uppercaseString];
    [fullKeys setObject:signKey forKey:@"sign"];
    return fullKeys;
}

- (NSString *)serializeKeys {
    NSArray *keys = [self.allKeys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2 options:NSLiteralSearch];
    }];
    NSString *soryKey = @"";
    for (NSString *key in keys) {
        soryKey = [soryKey stringByAppendingString:[NSString stringWithFormat:@"%@=%@&",key,[self objectForKey:key]]];
    }
    return [soryKey substringToIndex:soryKey.length - 1];
}

- (NSDictionary *)keysWithOther:(NSDictionary *)dic{
    NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] initWithDictionary:self];
    for (NSString *key in dic.allKeys) {
        [mutableDict setObject:[dic objectForKey:key] forKey:key];
    }
    return mutableDict;
}



@end
